<?php

namespace App\Http\Controllers\Api\Home;

use App\Http\Controllers\Api\Controller;
use App\Http\Requests\Api\AuthorizationRequest;
use App\Http\Requests\Api\SocialAuthorizationRequest;
use App\User;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Laravel\Socialite\Facades\Socialite;

//TODO  验证码和短信登录  不使用
class AuthorizationsController extends Controller
{
    //  status   是否过期0 1
    //  is_has    是否领取 0 1
    protected $giving = [
        ['day'=>1,'z_m'=>12,'status'=>'1','is_has'=>'0'],
        ['day'=>2,'z_m'=>8,'status'=>'1','is_has'=>'0'],
        ['day'=>3,'z_m'=>3,'status'=>'1','is_has'=>'0'],
        ['day'=>4,'z_m'=>5,'status'=>'1','is_has'=>'0'],
        ['day'=>5,'z_m'=>10,'status'=>'1','is_has'=>'0'],
        ['day'=>6,'z_m'=>3,'status'=>'1','is_has'=>'0'],
        ['day'=>7,'z_m'=>5,'status'=>'1','is_has'=>'0'],
        ['day'=>8,'z_m'=>10,'status'=>'1','is_has'=>'0'],
        ['day'=>9,'z_m'=>3,'status'=>'1','is_has'=>'0'],
        ['day'=>10,'z_m'=>5,'status'=>'1','is_has'=>'0'],
        ['day'=>11,'z_m'=>10,'status'=>'1','is_has'=>'0'],
        ['day'=>12,'z_m'=>2,'status'=>'1','is_has'=>'0'],
        ['day'=>13,'z_m'=>5,'status'=>'1','is_has'=>'0'],
        ['day'=>14,'z_m'=>2,'status'=>'1','is_has'=>'0'],
        ['day'=>15,'z_m'=>5,'status'=>'1','is_has'=>'0'],
        ['day'=>16,'z_m'=>2,'status'=>'1','is_has'=>'0'],
        ['day'=>17,'z_m'=>5,'status'=>'1','is_has'=>'0'],
        ['day'=>18,'z_m'=>2,'status'=>'1','is_has'=>'0'],
        ['day'=>19,'z_m'=>5,'status'=>'1','is_has'=>'0'],
        ['day'=>20,'z_m'=>2,'status'=>'1','is_has'=>'0'],
        ['day'=>21,'z_m'=>5,'status'=>'1','is_has'=>'0'],
        ['day'=>22,'z_m'=>2,'status'=>'1','is_has'=>'0'],
        ['day'=>23,'z_m'=>5,'status'=>'1','is_has'=>'0'],
        ['day'=>24,'z_m'=>2,'status'=>'1','is_has'=>'0'],
        ['day'=>25,'z_m'=>5,'status'=>'1','is_has'=>'0'],
        ['day'=>26,'z_m'=>2,'status'=>'1','is_has'=>'0'],
        ['day'=>27,'z_m'=>5,'status'=>'1','is_has'=>'0'],
        ['day'=>28,'z_m'=>2,'status'=>'1','is_has'=>'0'],
        ['day'=>29,'z_m'=>4,'status'=>'1','is_has'=>'0'],
        ['day'=>30,'z_m'=>2,'status'=>'1','is_has'=>'0'],
        ];
    public function socialStore($type, SocialAuthorizationRequest $request){
        if (in_array($type,['weixin'])){
            return $this->response->errorBadRequest();
        }
        $driver = Socialite::driver($type);
        try{
           if ($code = $request->code){
               $response = $driver->getAccessTokenRespones($code);
               $token = array_get($response,'access_token');
           }else{
               $token = $request->access_token;

               if ($type == 'weixin'){
                   $driver->setOpenId($request->openid);
               }
           }

           $oauthUser = $driver->userFromToken($token);
        }catch (\Exception $e){
            return $this->response->errorUnauthorized('参数异常');
        }

        switch ($type){
            case 'weixin';
                $unionid = $oauthUser->offsetExists('unionid') ? $oauthUser->offsetGet('unionid'):'';

                if ($unionid){
                    $user = User::where('weixin_unionid,$unionid')->first();
                }else{
                    $user = User::where('weixin_openid',$oauthUser->getId())->first();
                }

                if (!$user){
                    $user = User::create([
                       'name'=> $oauthUser->getNickname(),
                        'avatar' => $oauthUser->getAvatar(),
                        'weixin_openid' => $oauthUser->getId(),
                        'weixin_unionid' => $unionid,
                    ]);
                }
                break;
        }
        $token = Auth::guard('api')->formUser($user);
        return $this->responseWithToken($token)->setStatusCode(201);

    }


    public function store(AuthorizationRequest $request){
        $username = $request->username;
        $credentials['phone'] = $username;
        $credentials['password'] = $request->password;
        if (! $token = auth::guard('api')->attempt($credentials)){
            return $this->response->errorUnauthorized('用户名或者密码错误');
        }
        return $this->responseWithToken($token)->setStatusCode(201);

    }

    public function responseWithToken($token){
        return $this->response->array([
           'access_token' => $token,
            'token_type' => 'Bearer',
            'expires_in' => Auth::guard('api')->factory()->getTTL()*60,
        ]);
    }


    public function update(){
        $token = Auth::guard('api')->refresh();
        return $this->responseWithToken($token);
    }


    public function destroy(){
        Auth::guard('api')->logout();
        return $this->response->noContent();
    }

    public function test(){

    }
}
